package com.hmall.gateway.filter;

import lombok.Data;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.OrderedGatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.List;

@Component
public class PrintAnyGatewayFilterFactory extends AbstractGatewayFilterFactory<PrintAnyGatewayFilterFactory.Config> {
    @Override
    public GatewayFilter apply(Config config) {
//        return new GatewayFilter(){
//            @Override
//            public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//                System.out.println("执行了PrintAnyGatewayFilterFactory的pre....");
//                return chain.filter(exchange);
//            }
//        };

        return new OrderedGatewayFilter(new GatewayFilter() {
            @Override
            public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
                String a = config.getA();
                System.out.println("a执行了PrintAnyGatewayFilterFactory的pre...." + a);
                String b = config.getB();
                System.out.println("b执行了PrintAnyGatewayFilterFactory的pre...." + b);
                String c = config.getC();
                System.out.println("c执行了PrintAnyGatewayFilterFactory的pre...." + c);
                System.out.println("执行了PrintAnyGatewayFilterFactory的pre....a,b,c");
                return chain.filter(exchange);
            }
        }, 100);
    }

    /**
     * 配置类，用于存储和管理配置信息
     */
    @Data
    public static class Config {
        private String a; // 配置项a，用于存储第一个配置信息
        private String b; // 配置项b，用于存储第二个配置信息
        private String c; // 配置项c，用于存储第三个配置信息
    }

    /**
     * 重写父类方法，用于定义快捷键字段的顺序
     * 返回一个包含字段名的列表，这些字段名按照特定顺序排列
     *
     * @return 返回一个包含"a", "b", "c"的不可变列表，表示字段的显示顺序
     */
    @Override
    public List<String> shortcutFieldOrder() {
        return List.of("a", "b", "c");  // 使用Java 9+的List.of()方法创建不可变列表
    }

    public PrintAnyGatewayFilterFactory() {
        super(Config.class);
    }
    }

